package com.soecode.lyf.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Collect;
import com.soecode.lyf.entity.Comment;
import com.soecode.lyf.entity.User;
import com.soecode.lyf.service.SearchService;

@Controller
//@Scope("session") // 表示会话级别
@RequestMapping("/search") // url:/模块/资源/{id}/细分 /seckill/list
public class SearchController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SearchService searchservice;
	
	@ResponseBody
	@RequestMapping(value = "searchlesson", method = RequestMethod.GET)
	public Result Focuson(String name, String userid, Model model) {
		Result result = null;
		if(!StringUtils.isEmpty(name)&&!StringUtils.isEmpty(userid)){
			List<Collect> list = searchservice.SearchLesson(name);
			if(list.size()!=0) {
			model.addAttribute("Lesson",list);
			result = new Result(0,model,"查询成功");
			}else {
				result = new Result(1,"没有记录");	
			}
		}else {
			result = new Result(1,"缺少参数");
		}
		return result;
	};
	
	@ResponseBody
	@RequestMapping(value = "searchQuestion", method = RequestMethod.GET)
	public Result searchQuestion(String searchvalue, String userid, int page, int size, Model model) {
		Result result = null;
		if(!StringUtils.isEmpty(searchvalue)&&!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(page)&&!StringUtils.isEmpty(size)){
			PageHelper.startPage(page,size);
			List<Comment> list = searchservice.SearchQuestion(searchvalue);
			PageInfo<Comment> page1 = new PageInfo<Comment>(list);
			if(page1.getTotal()!=0) {
		    	/*System.out.println("总数量：" + page1.getTotal());
		    	System.out.println("当前页查询记录：" + page1.getList().size());
		    	System.out.println("当前页码：" + page1.getPageNum());
		    	System.out.println("每页显示数量：" + page1.getPageSize());
		    	System.out.println("总页：" + page1.getPages());*/
		        model.addAttribute("QUESTIONLIST",list);
		        model.addAttribute("PAGESUM",page1.getTotal());
		        result = new Result(0,model,"查询分页成功");
			}else {
				result = new Result(1,"没有记录");	
			}
		}else {
			result = new Result(1,"缺少参数");
		}
		return result;
	};
}
